home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / APPLICAT / C034.ZIP / DBQCOM.C < prev    next >
Text File  |  2010-11-01  |  12KB  |  676 lines

  1. /* SDB - boolean expression evaluator / compiler */
  2.  
  3. #include "bdscio.h"
  4. #include "dbqdefs.h"
  5.  
  6. /* The first section of code originated from sdbint.c */
  7.  
  8. int db_interpret(slptr)
  9.     struct sel *slptr;
  10. {
  11.     struct operand *result;
  12.     int r,inting;
  13.  
  14.  
  15.     if ((cptr = slptr->sl_where) == NULL)
  16.         return (TRUE);
  17.  
  18.     sptr = stack;
  19. #ifdef COMDEB
  20.         printf("com:interpret - entry\n");
  21. #endif
  22.  
  23. /*        patch out original
  24.  
  25.     while ((*(*cptr++).c_operator)())
  26. #ifdef COMDEB
  27.         printf("com:interpret - cptr loop\n")
  28. #endif
  29.         ;
  30.  
  31. end of patch        */
  32.  
  33.     /* own version */
  34.  
  35.     inting = TRUE;        /* Set for loop */
  36.     while (inting) {
  37. #ifdef COMDEB
  38.         printf("interpret - operator = %d\n",(*cptr).c_operator);
  39. #endif
  40.         switch ((*cptr++).c_operator) {
  41.             case XPUSH:
  42.                 *sptr++ = (*cptr++).c_operand;
  43.                 break;
  44.             case TKXAND:
  45.                 inting = boolean('&');
  46.                 break;
  47.             case TKXOR:
  48.                 inting = boolean('|');
  49.                 break;
  50.             case LSS:
  51.                 inting = compare(LSS);
  52.                 break;
  53.             case LEQ:
  54.                 inting = compare(LEQ);
  55.                 break;
  56.             case EQL:
  57.                 inting = compare(EQL);
  58.                 break;
  59.             case CONT:
  60.                 inting = compcont();
  61.                 break;
  62.             case GEQ:
  63.                 inting = compare(GEQ);
  64.                 break;
  65.             case GTR:
  66.                 inting = compare(GTR);
  67.                 break;
  68.             case NEQ:
  69.                 inting = compare(NEQ);
  70.                 break;
  71.             case XNOT:
  72.                 inting = db_xnot();
  73.                 break;
  74.             default:
  75.                 inting = FALSE;
  76.             }
  77.             
  78.     }    /* end while cptr loop */
  79.     result = *--sptr;
  80.     r = result->o_value.ov_boolean;
  81.     if (result->o_type == TEMP)
  82.         CFREE(result);
  83.  
  84.     while (sptr != stack) {
  85. #ifdef COMDEB
  86.         printf("com:interpret - stack loop\n");
  87. #endif
  88.         if ((*sptr)->o_type == TEMP)
  89.             CFREE(*sptr);
  90.         sptr -= 1;
  91.     }
  92.  
  93.     return (r);
  94. }
  95. /*    Patch out unwanted functions
  96. int db_xstop()
  97. {
  98.     return (FALSE);
  99. }
  100.  
  101. int db_xpush()
  102. {
  103.     *sptr++ = (*cptr++).c_operand;
  104. }
  105.  
  106. int db_xand()
  107. {
  108.     return (boolean('&'));
  109. }
  110.  
  111. int db_xor()
  112. {
  113.     return (boolean('|'));
  114. }
  115.  
  116. int db_xlss()
  117. {
  118.     return (compare(LSS));
  119. }
  120.  
  121. int db_xleq()
  122. {
  123.     return (compare(LEQ));
  124. }
  125.  
  126. int db_xeql()
  127. {
  128.     return (compare(EQL));
  129. }
  130.  
  131. int db_xgeq()
  132. {
  133.     return (compare(GEQ));
  134. }
  135.  
  136.  
  137. int db_xgtr()
  138. {
  139.     return (compare(GTR));
  140. }
  141.  
  142. int db_xneq()
  143. {
  144.     return (compare(NEQ));
  145. }
  146.  
  147. end of unwanted functions */
  148.  
  149. int boolean(opr)
  150. {
  151.     struct operand *lval,*rval,*result;
  152.     int lv,rv,r;
  153.  
  154.     rval = *--sptr; lval = *--sptr;
  155.     lv = lval->o_value.ov_boolean;
  156.     rv = rval->o_value.ov_boolean;
  157.     if ((result = CALLOC(OPSIZE)) == NULL)
  158.         { RETERR(INSMEM) }
  159.     result->o_type = TEMP;
  160.     switch (opr) {
  161.     case '&':    r = (lv && rv);
  162.             break;
  163.     case '|':    r = (lv || rv);
  164.             break;
  165.     }
  166. #ifdef COMDEB
  167.     printf("com:bool lv=%d,rv=%d,r=%d\n",lv,rv,r);
  168. #endif
  169.     result->o_value.ov_boolean = r;
  170.     *sptr++ = result;
  171.     if (lval->o_type == TEMP)
  172.         CFREE(lval);
  173.     if (rval->o_type == TEMP)
  174.         CFREE(rval);
  175.     return (TRUE);
  176. }
  177.  
  178. int db_xnot()
  179. {
  180.     struct operand *val,*result;
  181.  
  182.     val = *--sptr;
  183.     if ((result = CALLOC(OPSIZE)) == NULL)
  184.         { RETERR(INSMEM) }
  185.     result->o_type = TEMP;
  186.     result->o_value.ov_boolean = !val->o_value.ov_boolean;
  187.     *sptr++ = result;
  188.     if (val->o_type == TEMP)
  189.         CFREE(val);
  190.     return (TRUE);
  191. }
  192.  
  193. int compare(cmp)
  194. {
  195.     struct operand *lval,*rval,*result;
  196.     int i;
  197.  
  198.     rval = *--sptr; lval = *--sptr;
  199.     if ((result = CALLOC(OPSIZE)) == NULL)
  200.         { RETERR(INSMEM) }
  201.     result->o_type = TEMP;
  202.  
  203.     if (lval->o_value.ov_char.ovc_type == TCHAR)
  204.         i = comp(lval,rval);
  205.     else
  206.         i = ncomp(lval,rval);
  207. #ifdef COMDEB
  208.     printf("com:compare - cmp=%d\n",cmp);
  209. #endif
  210.     switch (cmp) {
  211.     case LSS:    i = (i < 0);
  212.             break;
  213.     case LEQ:    i = (i <= 0);
  214.             break;
  215.     case EQL:    i = (i == 0);
  216.             break;
  217.     case GEQ:    i = (i >= 0);
  218.             break;
  219.     case GTR:    i = (i > 0);
  220.             break;
  221.     case NEQ:    i = (i != 0);
  222.             break;
  223.     }
  224.     result->o_value.ov_boolean = i;
  225.     *sptr++ = result;
  226.     if (lval->o_type == TEMP)
  227.         CFREE(lval);
  228.     if (rval->o_type == TEMP)
  229.         CFREE(rval);
  230.     return (TRUE);
  231. }
  232.  
  233. int compcont(cmp)
  234. {
  235.     struct operand *lval,*rval,*result;
  236.     int i;
  237.  
  238.     rval = *--sptr; lval = *--sptr;
  239.     if ((result = CALLOC(OPSIZE)) == NULL)
  240.         { RETERR(INSMEM) }
  241.     result->o_type = TEMP;
  242.  
  243.     i = contains(lval,rval);
  244.  
  245. /*    printf("i after contains = %d\n",i);    */
  246.  
  247.     result->o_value.ov_boolean = i;
  248.     *sptr++ = result;
  249.     if (lval->o_type == TEMP)
  250.         CFREE(lval);
  251.     if (rval->o_type == TEMP)
  252.         CFREE(rval);
  253.     return (TRUE);
  254. }
  255.  
  256. int contains(lval,rval)
  257. struct operand *lval,*rval;
  258. {
  259.     char *lptr,*rptr;
  260.     int lctr,rctr;
  261.     int len;
  262.  
  263.     lptr = lval->o_value.ov_char.ovc_string;
  264.     lctr = lval->o_value.ov_char.ovc_length;
  265.     rptr = rval->o_value.ov_char.ovc_string;
  266.     rctr = rval->o_value.ov_char.ovc_length;
  267.  
  268.     while (lctr > 0 && (lptr[lctr-1] == 0 || lptr[lctr-1] == ' '))
  269.         lctr--;
  270.     while (rctr > 0 && (rptr[rctr-1] == 0 || rptr[rctr-1] == ' '))
  271.         rctr--;
  272.  
  273. /*    printf("lctr = %d, rctr = %d\n",lctr,rctr);    */
  274.     
  275.     if (lctr < rctr)
  276.         return(FALSE);
  277.     else
  278.         len = rctr;
  279.  
  280. /*    printf("length = %d\n",len);    */
  281.  
  282.     while (lctr >= rctr) {
  283.         if(db_sncmp(lptr,rptr,rctr) == 0) return(TRUE);
  284. /*    printf("forloop - lptr = %s\n",lptr);        */
  285.         lptr++;
  286.         lctr--;
  287.         }
  288. /*    printf("out of for loop\n");    */
  289.     return (FALSE);
  290. }
  291.  
  292. int comp(lval,rval)
  293.     struct operand *lval,*rval;
  294. {
  295.     char *lptr,*rptr; int lctr,rctr;
  296.     int len;
  297.  
  298.     lptr = lval->o_value.ov_char.ovc_string;
  299.     lctr = lval->o_value.ov_char.ovc_length;
  300.     rptr = rval->o_value.ov_char.ovc_string;
  301.     rctr = rval->o_value.ov_char.ovc_length;
  302. #ifdef COMDEB
  303.     printf("com:comp - lptr = %d,%d,%d,%d\n",lptr[6],lptr[7],lptr[8],lptr[9]);
  304. #endif
  305.     while (lctr > 0 && (lptr[lctr-1] == 0 || lptr[lctr-1] == ' '))
  306.         lctr--;
  307.     while (rctr > 0 && (rptr[rctr-1] == 0 || rptr[rctr-1] == ' '))
  308.         rctr--;
  309.  
  310.     if (lctr < rctr)
  311.         len = lctr;
  312.     else
  313.         len = rctr;
  314. #ifdef COMDEB
  315.     printf("com:comp - lctr=%d,rctr=%d\n",lctr,rctr);
  316. #endif
  317.  
  318.     while ((len--) > 0) {
  319.         if (*lptr++ != *rptr++)
  320.             if (*--lptr < *--rptr)
  321.                 return (-1);
  322.             else
  323.                 return (1);
  324.     }
  325.     if (lctr == rctr)
  326.         return (0);
  327.     else if (lctr < rctr)
  328.         return (-1);
  329.     else
  330.         return (1);
  331.  
  332. /*    return(db_sncmp(lptr,rptr,len));    */
  333. }
  334.  
  335. int ncomp(lval,rval)
  336.     struct operand *lval,*rval;
  337. {
  338.     char lstr[NUMBERMAX+1],rstr[NUMBERMAX+1];
  339.     int len;
  340.  
  341.     strncpy(lstr,lval->o_value.ov_char.ovc_string,
  342.         (len = lval->o_value.ov_char.ovc_length)); lstr[len] = EOS;
  343.  
  344.     strncpy(rstr,rval->o_value.ov_char.ovc_string,
  345.         (len = rval->o_value.ov_char.ovc_length)); rstr[len] = EOS;
  346.  
  347.  
  348.     return (db_cmp(lstr,rstr));
  349. }
  350.  
  351. /* The following originated from com.c */
  352.  
  353.  
  354. int db_compile(slptr)
  355.     struct sel *slptr;
  356. {
  357.     int result,i;
  358.     union codecell *cptr;
  359.     int (*dns)();
  360.  
  361. #ifdef COMDEB
  362.     printf("com:compile - entry\n");
  363. #endif
  364.  
  365.     selptr = slptr;
  366.  
  367.     cndx = 0;
  368.  
  369.     if (!expr(&result)) {
  370. #ifdef COMDEB
  371.         printf("com:compile - expr-error = %d\n",dbv_errcode);
  372. #endif
  373.         code[cndx++].c_operator = XSTOP;
  374.         freelit(code);
  375.         return (FALSE);
  376.     }
  377. #ifdef COMDEB
  378.     printf("com:compile - after expr call\n");
  379. #endif
  380.     code[cndx++].c_operator = XSTOP;
  381.  
  382.     if ((cptr = CALLOC(CELLSIZE * cndx)) == NULL) {
  383.         freelit(code);
  384.         return(FALSE);
  385.     }
  386.  
  387.     slptr->sl_where = cptr;
  388.     for (i = 0; i < cndx; i++) {
  389. #ifdef COMDEB
  390.         printf("com.compile - i = %d, op = %d\n",i,code[i].c_operator);
  391. #endif
  392.  
  393.         (*cptr++).c_operator = code[i].c_operator;
  394.         if (code[i].c_operator == XPUSH)
  395.             (*cptr++).c_operand = code[++i].c_operand;
  396.     }
  397.  
  398.     return (TRUE);
  399. }
  400.  
  401. db_fcode(slptr)
  402.     struct sel *slptr;
  403. {
  404. #ifdef COMDEB
  405.     printf("com:fcode - entry\n");
  406. #endif
  407.     if (slptr->sl_where == NULL)
  408.         return;
  409.  
  410.     freelit(slptr->sl_where);
  411.  
  412.     CFREE(slptr->sl_where);
  413. }
  414.  
  415. int operator(opr)
  416.     int *opr;
  417. {
  418. #ifdef COMDEB
  419.     printf("com:operator - entry\n");
  420. #endif
  421.     if (cndx < CODEMAX)
  422.         code[cndx++].c_operator = opr;
  423.     else
  424.         { RETERR(CDSIZE) }
  425.  
  426.     return (TRUE);
  427. }
  428.  
  429. int oprand(opr)
  430.     struct operand *opr;
  431. {
  432. #ifdef COMDEB
  433.     printf("com:oprand - entry\n");
  434. #endif
  435.     if (!operator(XPUSH))
  436.         return (FALSE);
  437.  
  438.     if (cndx < CODEMAX)
  439.         code[cndx++].c_operand = opr;
  440.     else
  441.         { RETERR(CDSIZE) }
  442.  
  443.     return (TRUE);
  444. }
  445.  
  446. int expr(result)
  447.     int *result;
  448. {
  449.     int lval,rval;
  450. #ifdef COMDEB
  451.     printf("com:expr - entry\n");
  452. #endif
  453.     if (!land(&lval))
  454.         return (FALSE);
  455.     while (db_token() == '|') {
  456.         db_ntoken();
  457.         if (!land(&rval))
  458.             return (FALSE);
  459.         if (!operator(TKXOR))
  460.             return (FALSE);
  461.     }
  462.     *result = lval;
  463.     return (TRUE);
  464. }
  465.  
  466. int land(result)
  467.     int *result;
  468. {
  469.     int lval,rval;
  470. #ifdef COMDEB
  471.     printf("com:land - entry\n");
  472. #endif
  473.     if (!relat(&lval))
  474.         return (FALSE);
  475.     while (db_token() == '&') {
  476.         db_ntoken();
  477.         if (!relat(&rval))
  478.             return (FALSE);
  479.         if (!operator(TKXAND))
  480.             return (FALSE);
  481.     }
  482.     *result = lval;
  483.     return (TRUE);
  484. }
  485.  
  486. int relat(result)
  487.     int *result;
  488. {
  489.     int lval,rval;
  490.     int tkn;
  491. #ifdef COMDEB
  492.     printf("com:relat - entry\n");
  493. #endif
  494.     if (!primary(&lval))
  495.         return (FALSE);
  496.     while (db_token() <= LSS && dbv_token >= GTR) {
  497.         tkn = db_ntoken();
  498. #ifdef COMDEB
  499.         printf("com:relat - tkn = %d\n",tkn);
  500. #endif
  501.         if (!primary(&rval))
  502.             return (FALSE);
  503.         if (!operator(tkn))
  504.             return (FALSE);
  505.     }
  506.     *result = lval;
  507.     return (TRUE);
  508. }
  509.  
  510. int primary(result)
  511.     int *result;
  512. {
  513.     int val;
  514.     int tkn;
  515. #ifdef COMDEB
  516.     printf("com:primary - entry\n");
  517. #endif
  518.     if (db_token() == '~') {
  519.         tkn = db_ntoken();
  520.         if (!primary(&val))
  521.             return (FALSE);
  522.         switch (tkn) {
  523.         case '~':
  524.             if (!operator(XNOT))
  525.                 return (FALSE);
  526.             break;
  527.         }
  528.     }
  529.     else
  530.         if (!factor(&val))
  531.             return (FALSE);
  532.     *result = val;
  533.     return (TRUE);
  534. }
  535.  
  536. int factor(result)
  537.     int *result;
  538. {
  539.     int val;
  540. #ifdef COMDEB
  541.     printf("com:factor - entry");
  542. #endif
  543.     if (db_token() == '(') {
  544.         db_ntoken();
  545.         if (!expr(&val))
  546.             return (FALSE);
  547.         if (db_token() != ')')
  548.             { RETERR(SYNTAX) }
  549.         db_ntoken();
  550.     }
  551.     else
  552.         if (!get_operand(&val))
  553.             return (FALSE);
  554.         *result = val;
  555.         return (TRUE);
  556. }
  557.  
  558.  
  559. int get_operand(result)
  560.     int *result;
  561. {
  562. #ifdef COMDEB
  563.     printf("com:getoper - entry\n");
  564. #endif
  565.     if (db_ntoken() == NUMBER)
  566.         return (get_thing(result,TNUM));
  567.     else if (dbv_token == ID)
  568.         return (get_attr(result));
  569.     else if (dbv_token == STRING)
  570.         return (get_thing(result,TCHAR));
  571.     else
  572.         { RETERR(SYNTAX) }
  573. }
  574.  
  575. int get_attr(result)
  576.     int *result;
  577. {
  578.     struct operand *opr;
  579.     char rname[RNSIZE+1],aname[ANSIZE+1];
  580.     char *aptr; int atype,alen;
  581.  
  582.     strncpy(aname,dbv_tstring,ANSIZE); aname[ANSIZE] = EOS;
  583.  
  584.     if (db_token() == '.') {
  585.         db_ntoken();
  586.  
  587.         strcpy(rname,aname);
  588.  
  589.         if (db_ntoken() != ID)
  590.             { RETERR(SYNTAX) }
  591.  
  592.         strncpy(aname,dbv_tstring,ANSIZE); aname[ANSIZE] = EOS;
  593.  
  594.         if (!db_sattr(selptr,rname,aname,&atype,&aptr,&alen))
  595.             return (FALSE);
  596.  
  597.     }
  598.     else
  599.         if (!db_sattr(selptr,NULL,aname,&atype,&aptr,&alen))
  600.             return (FALSE);
  601.  
  602.     if ((opr = CALLOC(OPSIZE)) == NULL)
  603.         { RETERR(INSMEM) }
  604.  
  605.     opr->o_type = ATTR;
  606.     opr->o_value.ov_char.ovc_type = atype;
  607.     opr->o_value.ov_char.ovc_string = aptr;
  608.     opr->o_value.ov_char.ovc_length = alen;
  609.  
  610.     if (!oprand(opr)) {
  611.         CFREE(opr);
  612.         return (FALSE);
  613.     }
  614.  
  615.     *result = atype;
  616.  
  617.     return (TRUE);
  618. }
  619.  
  620.  
  621.  
  622.  
  623.  
  624. int get_thing(result,tipe)
  625.     int *result; int tipe;
  626. {
  627.     struct operand *opr;
  628. #ifdef COMDEB
  629.     printf("com:getthing\n");
  630. #endif
  631.     if ((opr = CALLOC(OPSIZE)) == NULL)
  632.         { RETERR(INSMEM) }
  633.  
  634.     opr->o_type = LITERAL;
  635.     if ((opr->o_value.ov_char.ovc_string =
  636.         CALLOC(strlen(dbv_tstring)+1)) == NULL) {
  637.         CFREE(opr);
  638.         RETERR(INSMEM)
  639.     }
  640.     opr->o_value.ov_char.ovc_type = tipe;
  641.     strcpy(opr->o_value.ov_char.ovc_string,dbv_tstring);
  642.     opr->o_value.ov_char.ovc_length = strlen(dbv_tstring);
  643.  
  644.     if (!oprand(opr)) {
  645.         CFREE(opr->o_value.ov_char.ovc_string); CFREE(opr);
  646.         return (FALSE);
  647.     }
  648.  
  649.     *result = tipe;
  650.  
  651.     return (TRUE);
  652. }
  653.  
  654. freelit(cptr)
  655.     union codecell *cptr;
  656. {
  657.     int catch;
  658. #ifdef COMDEB
  659.     printf("com:freelit - entry\n");
  660. #endif
  661.     for (catch = 0; ((*cptr).c_operator != XSTOP) && (catch < CODEMAX); cptr++, catch++)
  662.         {
  663. #ifdef COMDEB
  664.         printf("com:freelit - op = %d, ty = %d\n",(*cptr).c_operator,
  665.                 (*cptr).c_operand->o_type);
  666. #endif
  667.         if ((*cptr).c_operator == XPUSH) {
  668.             if ((*++cptr).c_operand->o_type == LITERAL)
  669.                CFREE((*cptr).c_operand->o_value.ov_char.ovc_string);
  670.             CFREE((*cptr).c_operand); }
  671.         }
  672. #ifdef COMDEB
  673.     printf("exit\n");
  674. #endif
  675. }
  676. r).c_operand->o_value.ov